package com.ddx.service.impl;

import com.ddx.common.enums.ErrorCode;
import com.ddx.common.enums.IncomeType;
import com.ddx.common.exception.BusinessException;
import com.ddx.common.util.AliyunVideoUtil;
import com.ddx.common.util.AssertUtil;
import com.ddx.common.util.DateUtil;
import com.ddx.dao.AdvertVideoMapper;
import com.ddx.dao.IncomeFlowMapper;
import com.ddx.dao.ShortVideoMapper;
import com.ddx.dao.UserMapper;
import com.ddx.dto.ShortVideoDto;
import com.ddx.dto.UserDto;
import com.ddx.pojo.AdvertVideo;
import com.ddx.pojo.IncomeFlow;
import com.ddx.pojo.ShortVideo;
import com.ddx.pojo.User;
import com.ddx.service.IUnconcernedService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dudianbo
 * @ClassName: ${CLASSNAME}
 * @Description: ${DESCRIPTION}
 * @date 2018/11/11
 */
@Service
public class UnconcernedServiceImpl implements IUnconcernedService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    UserMapper userMapper;
    @Autowired
    ShortVideoMapper shortVideoMapper;
    @Autowired
    AdvertVideoMapper advertVideoMapper;
    @Autowired
    IncomeFlowMapper incomeFlowMapper;

    @Override
    public UserDto userCenter(String userId) {
        logger.info("未关注个人中心页面userId={}",userId);
        if(StringUtils.isBlank(userId)){
            AssertUtil.isNotNull(userId, "用户id为空");
        }
        UserDto userDto=new UserDto();
        User user=userMapper.selectByPrimaryKey(Integer.parseInt(userId));
        BeanUtils.copyProperties(user,userDto);
        return userDto;
    }

    @Override
    public UserDto releaseVideo(String userId) {
        logger.info("用户发布作品页面userId={}",userId);
        return null;
    }

    @Override
    public void uploadVideo(ShortVideoDto shortVideoDto, String userId) throws Exception{
        logger.info("用户上传作品userId={},shortVideoDto={}",userId,shortVideoDto);
        ShortVideo record=new ShortVideo();
        record.setAddress(shortVideoDto.getAddress());
        record.setContent(shortVideoDto.getContent());
        record.setUserId(Integer.parseInt(userId));
//        record.setVideoUrl(AliyunVideoUtil.getPlayInfo(shortVideoDto.getVideoUrl()));
//        record.setCoverUrl(AliyunVideoUtil.getCoverUrl(shortVideoDto.getVideoUrl()));
        record.setVideoId(shortVideoDto.getVideoUrl());
        record.setType(shortVideoDto.getType());
        shortVideoMapper.insertSelective(record);

        if (shortVideoDto.getAdvertId()!=null&&shortVideoDto.getAdvertId() > 0) {
            AdvertVideo advertVideo = new AdvertVideo();
            advertVideo.setAdvertId(shortVideoDto.getAdvertId());
            advertVideo.setVideoId(record.getId());
            advertVideoMapper.insertSelective(advertVideo);
        }
    }

    @Override
    public Map<String, Object> incomeFlow(String userId) {
        logger.info("我的收入入参{}",userId);
        IncomeFlow incomeFlow=new IncomeFlow();
        incomeFlow.setUserId(userId);
        List<IncomeFlow> list= incomeFlowMapper.selectByRecord(incomeFlow);
        List<Map<String,Object>> maps=new ArrayList<>();
        for(IncomeFlow income:list){
            Map<String,Object> map=new HashMap<>();
            map.put("type", IncomeType.getStatus(income.getType()).getMsg());
            map.put("content",income.getContent());
            map.put("createTime", DateUtil.format(income.getCreateTime(),DateUtil.YYYYMMDD_HHMMSS));
            maps.add(map);
        }
        Map<String,Object> result=new HashMap<>();
        result.put("incomeFlow",maps);
        return result;
    }

    @Override
    public void coverPrivate(ShortVideoDto shortVideoDto) {
        logger.info("转为私人视频入参{}",shortVideoDto);
        AssertUtil.isNotNull(shortVideoDto.getId(),"视频id为空");
        ShortVideo shortVideo=shortVideoMapper.selectByPrimaryKey(shortVideoDto.getId());
        if(!shortVideo.getUserId().equals(shortVideoDto.getUserId())){
            throw new BusinessException(ErrorCode.ACCOUNT_ERROR_309);
        }
        ShortVideo record=new ShortVideo();
        record.setId(shortVideoDto.getId());
        record.setStatus("private");
        shortVideoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void deleteVideo(ShortVideoDto shortVideoDto) {
        logger.info("删除视频入参{}",shortVideoDto);
        AssertUtil.isNotNull(shortVideoDto.getId(),"视频id为空");
        ShortVideo shortVideo=shortVideoMapper.selectByPrimaryKey(shortVideoDto.getId());
        if(!shortVideo.getUserId().equals(shortVideoDto.getUserId())){
            throw new BusinessException(ErrorCode.ACCOUNT_ERROR_309);
        }
        shortVideoMapper.deleteByPrimaryKey(shortVideoDto.getId());
    }
}
